package com.jright.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jright.po.Student;
import com.jright.po.Teacher;
import com.jright.po.Thesis;
import com.jright.po.Topic;
import com.jright.service.StudentService;
import com.jright.service.TeacherService;
import com.jright.service.ThesisService;
import com.jright.service.TopicService;

import net.sf.json.JSONObject;

@Controller
public class TeacherController {

	@Autowired
	private TeacherService teacherService;
	@Autowired
	private TopicService topicService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private ThesisService thesisService;
	
	@RequestMapping("/teacher/toIndex")
	public String toIndex(HttpSession session) {
		Teacher teacher = (Teacher) session.getAttribute("teacher");
		teacher = this.teacherService.findTeacherByNo(teacher.getNo());
		session.setAttribute("teacher", teacher);
		return "/teacher/index";
	}
	@RequestMapping("/teacher/toMember")
	public String toMember(HttpServletRequest request) {
		List<String> noList = this.studentService.findNoTeacherStudentNo();
		request.setAttribute("noList", noList);
		return "/teacher/member";
	}
	@RequestMapping("/teacher/toPublishtopic")
	public String toPublishtopic() {
		return "/teacher/publishtopic";
	}
	@RequestMapping("/teacher/toSuggestedit")
	public String toSuggestedit(HttpServletRequest request) {
		int id = Integer.parseInt(request.getParameter("thesis_id"));
		request.setAttribute("id", id);
		return "/teacher/suggestedit";
	}
	@RequestMapping("/teacher/toTopic")
	public String toTopic(HttpServletRequest request, HttpSession session) {
		Teacher teacher = (Teacher)session.getAttribute("teacher");
		List<Topic> teacherTopicList = this.topicService.teacherTopic(teacher.getNo());
		List<Topic> studentTopicList = new ArrayList<Topic>();
		List<Student> studentList = this.studentService.findStudentByTeacherNo(teacher.getNo());
		for(Student student : studentList) {
			for(Topic topic : student.getPublish_topicList()) {
				studentTopicList.add(topic);
			}
		}
		request.setAttribute("teacherTopicList", teacherTopicList);
		request.setAttribute("studentList", studentList);
		return "/teacher/topic";
	}
	@RequestMapping("/teacher/publishTopic")
	@ResponseBody
	public JSONObject publishTopic(@RequestBody String json) {
		JSONObject data = JSONObject.fromObject(json); 
		String title = data.getString("title"); 
		String publish_no = data.getString("publish_no"); 
		String introduction = data.getString("introduction"); 
		int row = this.topicService.publishTeacherTopic(title, publish_no, introduction);
		JSONObject json1 = new JSONObject();
		if(row > 0) {
			json1.put("msg","上传成功！");
		}else
			json1.put("msg","上传失败！");
		return json1;
	}
	
	@RequestMapping("/teacher/toStudentsituation")
	public String toStudentsituation(HttpServletRequest request, HttpSession session) {
		Teacher teacher = (Teacher) session.getAttribute("teacher");
		List<Student> studentList = this.studentService.findStudentByTeacherNo(teacher.getNo());
		for(Student students : studentList) {
			System.out.println(students.toString());
		}
		request.setAttribute("studentList", studentList);
		return "teacher/studentsituation";
	}
	@RequestMapping("/teacher/toStudentthesis")
	public String toStudentthesis(HttpServletRequest request) {
		String student_no = request.getParameter("student_no");
		List<Thesis> thesisList = this.thesisService.findThesisBySno(student_no);
		request.setAttribute("thesisList", thesisList);
		return "teacher/studentthesis";
	}
	
	@RequestMapping("/teacher/deleteStudent")
	@ResponseBody
	public JSONObject deleteStudent(@RequestBody String json) {
		JSONObject data = JSONObject.fromObject(json); 
		String student_no = data.getString("student_no"); 
		int row = this.studentService.updateStudentTeacher(null, student_no);
		JSONObject json1 = new JSONObject();
		if(row > 0) {
			json1.put("msg","删除成功！");
		}else
			json1.put("msg","删除失败！");
		return json1;
	}
	
	@RequestMapping("/teacher/addStudent")
	@ResponseBody
	public JSONObject addStudent(@RequestBody String json,HttpSession session) {
		Teacher teacher = (Teacher)session.getAttribute("teacher");
		JSONObject data = JSONObject.fromObject(json); 
		String student_no = data.getString("student_no"); 
		int row = this.studentService.updateStudentTeacher(teacher.getNo(), student_no);
		JSONObject json1 = new JSONObject();
		if(row > 0) {
			json1.put("msg","增加成功！");
		}else
			json1.put("msg","增加失败！");
		return json1;
	}
	
	@RequestMapping("/teacher/updateTopicIsagree")
	@ResponseBody
	public JSONObject updateTopicIsagree(@RequestBody String json) {
		JSONObject data = JSONObject.fromObject(json); 
		int topic_id = data.getInt("topic_id");
		String isagree = data.getString("isagree"); 
		int row = this.topicService.updateTopicIsagree(isagree, topic_id);
		JSONObject json1 = new JSONObject();
		if(row > 0) {
			json1.put("msg","修改成功！");
		}else
			json1.put("msg","修改失败！");
		return json1;
	}
	@RequestMapping("/teacher/addSuggest")
	@ResponseBody
	public JSONObject addSuggest(@RequestBody String json) {
		JSONObject data = JSONObject.fromObject(json); 
		String suggest = data.getString("suggest"); 
		int id = data.getInt("id");
		int row = this.thesisService.addSuggest(suggest, id);
		JSONObject json1 = new JSONObject();
		if(row > 0) {
			json1.put("msg","提交成功！");
		}else
			json1.put("msg","提交失败！");
		return json1;
	}
	@RequestMapping("/teacher/passTopic")
	@ResponseBody
	public JSONObject passTopic(@RequestBody String json) {
		JSONObject data = JSONObject.fromObject(json); 
		String no = data.getString("teacher_no"); 
		int row = this.studentService.wetherAllSelect(no);
		JSONObject json1 = new JSONObject();
		if(row > 0) {
			json1.put("msg","还有学生尚未选好论题，请先保证你的学生选好题目！");
		}else {
			List<Integer> topicIdList = this.studentService.findStudentTopicId(no);
			for(int topic_id : topicIdList) {
				System.out.println(topic_id);
				this.topicService.updateTopicStatus("审核中", topic_id);
				Student student = this.studentService.findStudentByTopicId(topic_id);
				this.studentService.updateStudentStatus("审核中", student.getNo());
				this.teacherService.updateIspost(no);
			}
			json1.put("msg","提交成功！");
		}
		return json1;
	}
	
	@RequestMapping("jury/toIndex")
	public String toJuryIndex(HttpServletRequest request) {
		List<Teacher> teacherList = this.teacherService.findPostTeacher();
		request.setAttribute("teacherList", teacherList);
		return "jury/index";
	}
	@RequestMapping("/jury/toStudentsituation")
	public String toJuryStudentsituation(HttpServletRequest request) {
		String teacher_no = request.getParameter("teacher_no");
		List<Student> studentList = this.studentService.findStudentByTeacherNo(teacher_no);
		for(Student students : studentList) {
			System.out.println(students.toString());
		}
		request.setAttribute("studentList", studentList);
		request.setAttribute("teacher_no", teacher_no);
		return "jury/studentsituation";
	}
	
	@RequestMapping("/jury/passTopic")
	@ResponseBody
	public JSONObject jutyPassTopic(@RequestBody String json) {
		JSONObject data = JSONObject.fromObject(json); 
		String no = data.getString("teacher_no"); 
		JSONObject json1 = new JSONObject();
		List<Integer> topicIdList = this.studentService.findStudentTopicId(no);
		for(int topic_id : topicIdList) {
			System.out.println("topic_id"+topic_id);
			this.topicService.updateTopicStatus("审核通过", topic_id);
			Student student = this.studentService.findStudentByTopicId(topic_id);
			this.studentService.updateStudentStatus("审核通过", student.getNo());
			this.teacherService.updateIspost(no);
		}
		json1.put("msg","提交成功！");
		return json1;
	}

}
